﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace personalplaner.common.controls
{
	[TemplatePart(Name = "PART_Popup", Type = typeof(UIElement))]
	public class MenuButton : Button
	{
		static MenuButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(MenuButton), new FrameworkPropertyMetadata(typeof(MenuButton)));
        }

		//static object syncRoot = new object();
        Popup popup;
		//Border popupButtonBorder;

        public MenuButton()
        {
			//FontSize = 9;
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            popup = GetTemplateChild("PART_Popup") as Popup;
			//popupButtonBorder = GetTemplateChild("PART_PopupButtonBorder") as Border;
        }

        public object Icon
        {
            get { return (object)GetValue(IconProperty); }
            set { SetValue(IconProperty, value); }
        }

        public static readonly DependencyProperty IconProperty =
            DependencyProperty.Register("Icon", typeof(object), typeof(MenuButton),
            new FrameworkPropertyMetadata(null, new CoerceValueCallback(OnCoerce_Icon)));

        private static object OnCoerce_Icon(DependencyObject o, object value)
        {
            if (value is string)
            {
                Uri iconUri;
                // try to resolve given value as an absolute URI
                if (Uri.TryCreate(value as String, UriKind.RelativeOrAbsolute, out iconUri))
                {
                    ImageSource img = new BitmapImage(iconUri);
                    if (null != img)
                    {
                        Image icon = (o as MenuButton).Icon as Image;
                        if (null == icon)
                            icon = new Image();

                        icon.Source = img;
                        icon.Stretch = Stretch.None;
                        icon.SnapsToDevicePixels = true;

                        return icon;
                    }
                }
            }
            return value;
        }

        public string Text
        {
            get
            {
                return (string)GetValue(TextProperty);
            }
            set
            {
                SetValue(TextProperty, value);
            }
        }

        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(MenuButton), new PropertyMetadata(""));

        protected override void OnClick()
        {
			//if (popupButtonBorder != null && popupButtonBorder.IsMouseOver)
			//{
			//    System.Diagnostics.Debug.WriteLine(popupButtonBorder.IsMouseOver.ToString());
			//    ShowPopup();
			//}
			//else
			//{
			//    if (popupButtonBorder != null &&
			//        popupButtonBorder.Visibility != System.Windows.Visibility.Visible &&
			//        popup != null &&
			//        Content != null)
			//        ShowPopup();

			//    base.OnClick();
			//}

			if (Content != null)
				ShowPopup();

			base.OnClick();
        }

        public void ShowPopup()
        {
            if (popup != null)
                popup.IsOpen = true;
        }

        public void HidePopup()
        {
            if (popup != null)
                popup.IsOpen = false;
        }
	}
}
